home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / bbsckit.zip / UMODEM.C < prev   
Text File  |  1980-01-01  |  45KB  |  1,414 lines

  1.  
  2. /*
  3.  *  UMODEM Version 3.7
  4.  *
  5.  *  UMODEM -- Implements the "CP/M User's Group XMODEM" protocol, 
  6.  *        the TERM II File Transfer Protocol (FTP) Number 1,
  7.  *        and the TERM II File Transfer Protocol Number 4 for
  8.  *        packetized file up/downloading.    
  9.  *
  10.  *    Note: UNIX System-Dependent values are indicated by the string [SD]
  11.  *      in a comment field on the same line as the values.
  12.  *
  13.  *
  14.  *     -- Lauren Weinstein, 6/81
  15.  *     -- (Version 2.0) Modified for JHU/UNIX by Richard Conn, 8/1/81
  16.  *     -- Version 2.1 Mods by Richard Conn, 8/2/81
  17.  *          . File Size Included on Send Option
  18.  *     -- Version 2.2 Mods by Richard Conn, 8/2/81
  19.  *          . Log File Generation and Option Incorporated
  20.  *     -- Version 2.3 Mods by Richard Conn, 8/3/81
  21.  *          . TERM II FTP 1 Supported
  22.  *          . Error Log Reports Enhanced
  23.  *     -- Version 2.4 Mods by Richard Conn, 8/4/81
  24.  *          . 16K-extent sector number check error corrected
  25.  *          . Count of number of received sectors added
  26.  *     -- Version 2.5 Mods by Richard Conn, 8/5/81
  27.  *          . ARPA Net Flag added
  28.  *          . ARPA Net parameter ('a') added to command line
  29.  *          . ARPA Net BIS, BIE, BOS, BOE added
  30.  *          . ARPA Net FFH escape added
  31.  *     -- Version 2.6 Mods by Bennett Marks, 8/21/81 (Bucky @ CCA-UNIX)
  32.  *          . mods for UNIX V7 (Note: for JHU compilation define
  33.  *        the variable JHU  during 'cc'
  34.  *          . added 'mungmode' flag to protect from inadvertant
  35.  *        overwrite on file receive
  36.  *          . changed timeout handling prior to issuing checksum
  37.  *     -- Version 2.7 Mods by Richard Conn, 8/25/81 (rconn @ BRL)
  38.  *          . correct minor "ifndef" error in which ifndef had no arg
  39.  *          . restructured "ifdef" references so that other versions
  40.  *        of UNIX than Version 7 and JHU can be easily incorporated;
  41.  *        previous ifdef references were for JHU/not JHU;
  42.  *        to compile under Version 7 or JHU UNIX, the following
  43.  *        command lines are recommended:
  44.  *              "cc umodem.c -o umodem -DVER7" for Version 7
  45.  *              "cc -7 umodem.c -o umodem -DJHU" for JHU
  46.  *          . added 'y' file status display option; this option gives
  47.  *        the user an estimate of the size of the target file to
  48.  *        send from the UNIX system in terms of CP/M records (128
  49.  *        bytes) and Kbytes (1024 byte units)
  50.  *          . added '7' option which modifies the transmission protocols
  51.  *        for 7 significant bits rather than 8; modifies both FTP 1
  52.  *        and FTP 3
  53.  *     -- Version 2.8 Mods by Richard Conn, 8/28/81
  54.  *          . corrected system-dependent reference to TIOCSSCR (for
  55.  *        disabling page mode) and created the PAGEMODE flag which
  56.  *        is to be set to TRUE to enable this
  57.  *          . added -4 option which engages TERM II, FTP 4 (new release)
  58.  *     -- Version 2.9 Mods by Richard Conn, 9/1/81
  59.  *          . internal documentation on ARPA Net protocols expanded
  60.  *          . possible operator precedence problem with BITMASK corrected
  61.  *        by redundant parentheses
  62.  *     -- Version 3.0 Mods by Lauren Weinstein, 9/14/81
  63.  *          . fixed bug in PAGEMODE defines (removed PAGEMODE define
  64.  *        line; now should be compiled with "-DPAGEMODE" if
  65.  *        Page Mode is desired)
  66.  *          . included forward declaration of ttyname() to avoid problems
  67.  *        with newer V7 C compilers
  68.  *     -- Version 3.1 Mods by Lauren Weinstein, 4/17/82
  69.  *          . avoids sending extraneous last sector when file EOF
  70.  *        occurs on an exact sector boundary
  71.  *     -- Version 3.2 Mods by Michael M Rubenstein, 5/26/83
  72.  *          . fixed bug in readbyte.  assumed that int's are ordered
  73.  *        from low significance to high
  74.  *          . added LOGDEFAULT define to allow default logging to be
  75.  *        off.  compile with -DLOGDEFAULT=0 to set default to no
  76.  *        logging.
  77.  *     -- Version 3.3 Mod by Ben Goldfarb, 07/02/83
  78.  *          . Corrected problem with above implementation of "LOGDEFAULT".
  79.  *        A bitwise, instead of a logical negation operator was
  80.  *        used to complement LOGFLAG when the '-l' command line
  81.  *        flag was specified.  This can cause LOGFLAG to be true
  82.  *        when it should be false.
  83.  *     -- Version 3.4 Mods by David F. Hinnant, NCECS, 7/15/83
  84.  *          . placed log file in HOME directory in case user doesn't
  85.  *        have write permission in working directory.
  86.  *          . added DELDEFAULT define to allow default purge/no purge
  87.  *        of logfile before starting.  Compile with -DDELDEFAULT=0
  88.  *        to set default to NOT delete the log file before starting.
  89.  *          . check log file for sucessful fopen().
  90.  *          . buffer disk read for sfile().
  91.  *          . turn messages off (standard v7) before starting.
  92.  *     -- Version 3.5 Mods by Richard Conn, 08/27/83
  93.  *          . added equates for compilation under UNIX SYSTEM III
  94.  *              to compile for SYSTEM III, use -DSYS3 instead of
  95.  *              -DJHU or -DVER7
  96.  *          . added command mode (-c option) for continuous entry
  97.  *              of commands
  98.  *     -- Version 3.6 Mods by Ben Goldfarb (ucf-cs!goldfarb), 09/03/83
  99.  *          . added '#include <ctype.h>' since tolower() is used, but
  100.  *        is not defined in umodem.  This is necessary to compile
  101.  *        on V7 systems.  Also added a isupper() test because 
  102.  *        tolower() in /usr/include/ctype.h doesn't do that.
  103.  *          . cleaned up all the improper bitwise complementation of
  104.  *        logical constants and variables.
  105.  *     -- Version 3.7 Mods by Noel J. Bergman, 02/27/84
  106.  *          . Corrected problem with ALARM signal in 4.2 BSD Unix.
  107.  *        BSD Unix restarts system calls after signal is handled,
  108.  *        so setjmp() and longjmp() are used to handle I/O timeout.
  109.  *        Since this will work with all Unix systems, and is a lot
  110.  *        cleaner than depending on side effects, there is no need
  111.  *        to make this code conditional.
  112.  *
  113.  */
  114.  
  115. #include <stdio.h>
  116. #include <sys/types.h>
  117. #include <sys/stat.h>
  118. #include <setjmp.h>
  119. #include <ctype.h>
  120.  
  121. /*  JHU UNIX tty parameter file  */
  122. #ifdef JHU
  123. #include <stty.h>
  124. #endif
  125.  
  126. /*  Version 7 UNIX tty parameter file  */
  127. #ifdef VER7
  128. #include <sgtty.h>
  129. #endif
  130.  
  131. /*  UNIX SYSTEM III tty parameter file  */
  132. #ifdef SYS3
  133. #include <sgtty.h>
  134. #endif
  135.  
  136. /* log default define */
  137. #ifndef LOGDEFAULT
  138. #define LOGDEFAULT      1
  139. #endif
  140.  
  141. /* Delete logfile define.  Useful on small systems with limited
  142.  * filesystem space and careless users.
  143.  */
  144. #ifndef DELDEFAULT
  145. #define DELDEFAULT      1
  146. #endif
  147.  
  148. #include <signal.h>
  149.  
  150. #define      VERSION    37      /* Version Number */
  151. #define      FALSE      0
  152. #define      TRUE       1 
  153.  
  154. /*  Compile with "-DPAGEMODE" if Page Mode (TIOCSSCR) is supported on your
  155.  *  UNIX system.  If it is supported, make sure that TIOCSSCR is the
  156.  *  correct name for Page Mode engagement.
  157.  */
  158.  
  159. /*  ASCII Constants  */
  160. #define      SOH    001 
  161. #define      STX    002
  162. #define      ETX    003
  163. #define      EOT    004
  164. #define      ENQ    005
  165. #define      ACK    006
  166. #define      LF     012   /* Unix LF/NL */
  167. #define      CR     015  
  168. #define      NAK    025
  169. #define      SYN    026
  170. #define      CAN    030
  171. #define      ESC    033
  172. #define      CTRLZ      032   /* CP/M EOF for text (usually!) */
  173.  
  174. /*  UMODEM Constants  */
  175. #define      TIMEOUT    -1
  176. #define      ERRORMAX   10    /* maximum errors tolerated */
  177. #define      RETRYMAX   10    /* maximum retries to be made */
  178. #define      BBUFSIZ    128   /* buffer size -- do not change! */
  179.  
  180. /*  [SD] Mode for Created Files  */
  181. #define      CREATMODE  0600  /* mode for created files */
  182.  
  183. /*  ARPA Net Constants  */
  184. /*      The following constants are used to communicate with the ARPA
  185.  *      Net SERVER TELNET and TIP programs.  These constants are defined
  186.  *      as follows:
  187.  *          IAC             <-- Is A Command; indicates that
  188.  *                          a command follows
  189.  *          WILL/WONT           <-- Command issued to SERVER TELNET
  190.  *                          (Host); WILL issues command
  191.  *                          and WONT issues negative of
  192.  *                          the command
  193.  *          DO/DONT         <-- Command issued to TIP; DO issues
  194.  *                          command and DONT issues
  195.  *                          negative of the command
  196.  *          TRBIN           <-- Transmit Binary Command
  197.  *      Examples:
  198.  *          IAC WILL TRBIN  <-- Host is configured to transmit Binary
  199.  *          IAC WONT TRBIN  <-- Host is configured NOT to transmit binary
  200.  *          IAC DO TRBIN    <-- TIP is configu